﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions administrateur

        ///////////////////// FONCTIONS ADMINISTRATEUR /////////////////////////////////

        //Listing des admin enregistrés
        private void list_administrators_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (Administrators s in (from e in db_local.Administrators select e))
            {
                maListe.Items.Add(s.Login);

            }
        }


        //Affichage de la liste des admin
        private void display_administrators_offline(String administrators)
        {
            foreach (Administrators s in (from e in db_local.Administrators select e))
            {
                if (s.Login == administrators)
                {
                    admin_login_textbox.Text = s.Login;
                    admin_password_textbox.Password = s.Password;
                    admin_firstname_textbox.Text = s.Firstname;
                    admin_surname_textbox.Text = s.Surname;
                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_administrators_offline()
        {
            admin_login_textbox.Text = "";
            admin_password_textbox.Password = "";
            admin_firstname_textbox.Text = "";
            admin_surname_textbox.Text = "";
            list_administrators_offline(admin_listbox);
        }


        //Ajout d'un admin
        private void add_administrators_offline()
        {
            Administrators admin1 = new Administrators();

            admin1.Login = admin_login_textbox.Text.ToLower();
            admin1.Password = admin_password_textbox.Password;
            admin1.Firstname = admin_firstname_textbox.Text.ToLower();
            admin1.Surname = admin_surname_textbox.Text.ToLower();

            admin1.Notepad_enabled = 1;
            admin1.Notepad_text = "Write some notes here";

            db_local.Administrators.InsertOnSubmit(admin1);
            db_local.SubmitChanges();

            //Rafraichissement des éléments
            refresh_administrators_offline();


        }


        //Mise à jour d'un admin
        private void update_administrators_offline()
        {
            if (admin_login_textbox.Text != "" && admin_password_textbox.Password != "" && admin_firstname_textbox.Text != "" && admin_surname_textbox.Text != "")
            {
                Administrators admin2 = (from e in db_local.Administrators
                                         where e.Login == (string)admin_listbox.SelectedValue
                                         select e).FirstOrDefault();

                admin2.Login = admin_login_textbox.Text.ToLower();
                admin2.Password = admin_password_textbox.Password;
                admin2.Firstname = admin_firstname_textbox.Text.ToLower();
                admin2.Surname = admin_surname_textbox.Text.ToLower();


                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_administrators_offline();
            }

        }


        //Suppression d'un admin
        private void delete_administrators_offline()
        {
            if (admin_login_textbox.Text != "")
            {
                Administrators admin3 = (from e in db_local.Administrators
                                         where e.Login == (string)admin_listbox.SelectedValue
                                         select e).FirstOrDefault();

                db_local.Administrators.DeleteOnSubmit(admin3);
                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_administrators_offline();
            }

        }

        #endregion
    }
}
